<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="500px"
    append-to-body
  >
    <div class="form-box new_form_box">
      <el-form
        ref="formAdd"
        :model="form"
        :inline="true"
        :rules="rules"
        label-width="60px"
        class="demo-ruleForm"
      >
      <!-- 类型  编号 手机号码 保管人员 备注 -->
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" >
            <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.name"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入渠道名称" />
        </el-form-item>
        <!-- <el-form-item label="手机号码" prop="tel">
          <span v-if="form.deviceType==2">{{form.tel}}</span>
          <el-input v-model="form.tel" v-else placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="保管人员" prop="custodian">
          <el-input v-model="form.custodian" placeholder="请输入保管人员" />
        </el-form-item> -->
     
        <el-row>
          <el-form-item class="row" label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请填写备注"
              :rows="4"
            />
          </el-form-item>
        </el-row>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('formAdd')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import otherChannels from "@/api/retrieveManage/otherChannels"
export default {
  name: "addFina",
  data() {
    var validatePass = (rule, value, callback) => {
    
        if (value&&value.length!=11) {
          callback(new Error('请输入11位手机号'));
        } else {
          callback();
        }
      };
    return {
      dialogVisible: false,
      loading: false,
      title: "新增信息",
      isEdit: false,
      typeList:[
        {
          name:'包赔商',
          value:1
        },
        {
          name:'平台渠道',
          value:2
        },
        {
          name:'供货商',
          value:3
        },
      ],
      // 表单参数
      form: {
        type: 1,//1包赔商  2平台
        name: "",
        remark: "",
      },
      rules: {
        type: [
          { required: true, message: "请选择类型", trigger: "change" }
        ],
        name: [
          { required: true, message: "请输入", trigger: "change" }
        ],
        tel: [
        { required: true, message: "请输入手机号", trigger: "blur" },
        { validator: validatePass, trigger: 'blur' }
        ],
      }
    };
  },
  created() {
  },
  methods: {
    // 打开弹窗
    open(flag, data) {
      this.form={
        type: 1,//1包赔商  2平台
        name: "",
        remark: "",
      }
      if (flag == "edit") {
        this.title = "修改信息";
        this.isEdit = true;
        this.handleUpdate(JSON.parse(JSON.stringify(data)));
      } else {
        if(data){
          this.form.type = data
        }
        this.isEdit = false;
      }
      this.dialogVisible = true;
    },

    // 重置
    reset() {
      this.$refs.formAdd.resetFields();
    },

    /** 查询详情进行回显 */
    handleUpdate(row) {
      this.form = row
    },
    submitForm(formName) {
      this.$refs["" + formName].validate(valid => {
        if (valid) {
          if (this.isEdit) {
            // 编辑
            otherChannels.editQudao(this.form).then(res => {
              this.$message.success(res.msg);
              this.reset()
              this.dialogVisible = false;
              this.$parent.getList();
            });

          } else {
            // 新增
            //console.log(this.form);
            otherChannels.addQudao(this.form).then(res => {
              this.$message.success(res.msg);
              this.reset()
              this.dialogVisible = false;
              if(this.$parent.getList){
                this.$parent.getList();
              }else{
                this.$emit('add')
              }
              
            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },

    close() {
      this.reset()
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}
.form-box {
  padding-top: 20px;
}

</style>
